package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.constants.CertificationConstants;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
* @author lizhijun
* @description 针对表【agency_certification_audit(机构认证审核表)】的数据库操作Service实现
* @createDate 2025-09-26 16:39:19
*/
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit>
    implements IAgencyCertificationAuditService {
    @Resource
    private IAgencyCertificationService iAgencyCertificationService;

    @Override
    public Page<AgencyCertificationAudit> pageAudit(AgencyCertificationAuditPageQueryReqDTO pageQueryReqDTO) {
        Page<AgencyCertificationAudit> page = new Page<>(pageQueryReqDTO.getPageNo(), pageQueryReqDTO.getPageSize());
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(pageQueryReqDTO.getAuditStatus() != null, AgencyCertificationAudit::getAuditStatus, pageQueryReqDTO.getAuditStatus());
        queryWrapper.eq(pageQueryReqDTO.getCertificationStatus() != null, AgencyCertificationAudit::getCertificationStatus, pageQueryReqDTO.getCertificationStatus());
        queryWrapper.like(StringUtils.hasText(pageQueryReqDTO.getLegalPersonName()), AgencyCertificationAudit::getLegalPersonName, pageQueryReqDTO.getLegalPersonName());
        queryWrapper.like(StringUtils.hasText(pageQueryReqDTO.getName()), AgencyCertificationAudit::getName, pageQueryReqDTO.getName());
        return super.page(page, queryWrapper);

    }

    @Override
    public RejectReasonResDTO getRejectReason() {
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgencyCertificationAudit::getAuditStatus, CertificationConstants.CERTIFICATION_STATUS_FAIL)
                .eq(AgencyCertificationAudit::getName, UserContext.currentUser().getName());
        AgencyCertificationAudit AgencyCertificationAudit = super.getOne(queryWrapper);
         return new RejectReasonResDTO(AgencyCertificationAudit.getRejectReason());
    }

    @Override
    public void audit(Long id, Integer certificationStatus, String rejectReason) {
        AgencyCertificationAudit agencyCertificationAudit = super.getById(id);
        if (agencyCertificationAudit == null){
            throw new RuntimeException("机构认证审核不存在");
        }
        super.update(Wrappers.<AgencyCertificationAudit>lambdaUpdate()
                .set(AgencyCertificationAudit::getAuditStatus,certificationStatus)
                .set(AgencyCertificationAudit::getRejectReason,rejectReason)
                .eq(AgencyCertificationAudit::getId,id));
        //修改认证信息表
        AgencyCertification agencyCertification = new AgencyCertification();
        agencyCertification.setId(agencyCertificationAudit.getServeProviderId());
        agencyCertification.setCertificationStatus(certificationStatus);
        iAgencyCertificationService.updateById(agencyCertification);
    }
}




